//
//  CZStatus.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/27.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

class CZStatus: NSObject {
        /// 创建时间
    var created_at: String?
        /// 微博id
    var id: Int64 = 0
        /// 内容
    var text: String?
        /// 配图数组
    var pic_urls:[CZPictureInfo]?
        /// 来源
    var source: String? {
        didSet {
            sourceArr = getSourceStr(source)
        }
    }
    
    var sourceArr: NSMutableAttributedString?
    
        /// 用户信息
    var user: CZUsers?
        ///转发数
    var reposts_count: Int = 0 {
        didSet {
            repostsStr = getCountString(reposts_count, title: "转发")
        }
    }
        /// 评论数
    var comments_count: Int = 0 {
        didSet {
            
            commentsStr = getCountString(comments_count, title: "评论")
        }
    }
        /// 表态数
    var attitudes_count: Int = 0 {
        didSet {
            attitudesStr = getCountString(attitudes_count, title: "赞")
            
        }
    }
    
        /// 转发
    var repostsStr: String?
        /// 评论
    var commentsStr: String?
        /// 表态
    var attitudesStr: String?
        /// 转发微博内容
    var retweeted_status: CZStatus?
    
    
    
    
    func getCountString(count: Int,title: String) -> String {
        if count == 0 {
            return title
        } else if count > 0 && count < 10000 {
            return "\(count)"
        } else {
            
            let cou = CGFloat(count / 1000) / 10
            var countStr = "\(cou)"
            
            if countStr.containsString(".0") {
                countStr = countStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return "\(countStr)万"
            
        }
    }
    
    // MARK - 处理微博来源的方法
    func getSourceStr(source: String?)-> NSMutableAttributedString {
        guard let s = source where s.containsString("\">") else {
            return getSourceArr("")
        }
        let start = s.rangeOfString("\">")!
        let end = s.rangeOfString("</a")!
        
        let result = s.substringWithRange(start.endIndex..<end.startIndex)
        let r = "来自 \(result)"
        
        return getSourceArr(r)
    }
    
    // MARK - 通过字符串生成富文本
    func getSourceArr(result: String)->NSMutableAttributedString {
        //实例化一个富文本
        let arr = NSMutableAttributedString(string: result)
        
        let range = (result as NSString).rangeOfString("来自")
        
        arr.addAttributes([NSForegroundColorAttributeName:ThemeColor], range: range)
        return arr
        
    }

    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            guard let v = value as? [String: AnyObject] else {
                return
            }
            let u = CZUsers(dict: v)
            user = u
        } else if key == "retweeted_status" {
            guard let v = value as? [String: AnyObject] else {
                return
            }
            let retweeted = CZStatus(dict: v)
            retweeted_status = retweeted
            
        } else if key == "pic_urls" {
            guard let v = value as? [[String: AnyObject]] else {
                return
            }
            var tempArray:[CZPictureInfo] = [CZPictureInfo]()
            
            for dict in v {
                let pict = CZPictureInfo(dict: dict)
                tempArray.append(pict)
            }
            pic_urls = tempArray
        } else {
            super.setValue(value, forKey: key)
        }
        
    }
    
}
